package controller

import (
	"fx_swift_forex/api"
	"fx_swift_forex/internal/model"
	"fx_swift_forex/internal/service"
	"fx_swift_forex/pkg/code"
	"fx_swift_forex/pkg/internalerrors"
	"github.com/gin-gonic/gin"
	"github.com/gogf/gf/v2/frame/g"
	"net/http"
)

func CreateCustomer(c *gin.Context) {
	req := &api.ComplyOnboardingReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	result, err := service.NewComplySrv().CreateCustomer(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 提交公司KYC资料
func PostCompanyCustomer(c *gin.Context) {
	req := &service.ComplyCompanyOnboardingReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err := service.NewComplySrv().PostCompanyCustomer(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

// 提交个人KYC资料
func PostPersonCustomer(c *gin.Context) {
	req := &model.ComplyPersonOnboardingReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err := service.NewComplySrv().PostPersonCustomer(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

// KYC认证资料保存
func CreateCustomerTemp(c *gin.Context) {
	req := &api.CustomerTempReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}

	err := service.NewComplySrv().CreateCustomerTemp(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

// 获取KYC认证资料
func GetCustomerTemp(c *gin.Context) {
	result, err := service.NewComplySrv().GetCustomerTemp(c)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 获取当前KYC客户入职状态信息
func GetCustomerMessage(c *gin.Context) {
	result, err := service.NewComplySrv().GetCustomerMessage(c)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 企业信息切换
func ChangeCompanyInfo(c *gin.Context) {
	customerId := c.Query("customer_id")
	account := c.Value("Account").(string)
	if err := g.Redis().SetEX(c, account+"_customerId", customerId, 174600); err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

// 获取预期收/付款总额
func GetPaymentInfo(c *gin.Context) {
	result, err := service.NewComplySrv().GetPaymentInfo(c)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 获取账号收付款用途
func GetPaymentPurposeInfo(c *gin.Context) {
	result, err := service.NewComplySrv().GetPaymentPurposeInfo(c)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 获取收入来源
func GetIncomeSourceInfo(c *gin.Context) {
	result, err := service.NewComplySrv().GetIncomeSourceInfo(c)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 验证企业邮箱是否唯一
func CheckEmail(c *gin.Context) {
	email := c.Query("email")
	result, err := service.NewComplySrv().CheckEmail(c, email)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}
